﻿using B.S.RbacData.Domain.Traceabilitys;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Read.API.Applications.Commands.Traceabilitys;
using MediatR;

namespace B.S.RbacData.Read.API.Applications.CommandHandlters.Traceabilitys
{
    /// <summary>
    /// 获取图片信息命令处理
    /// </summary>
    public class GetAllImgsCommandhandlter : IRequestHandler<GetAllImgsCommand, ApiResult<List<ImagesModel>>>
    {
        private readonly IBaseRepository<ImagesModel> imgsRep;

        public GetAllImgsCommandhandlter(IBaseRepository<ImagesModel> imgsRep)
        {
            this.imgsRep = imgsRep;
        }

        /// <summary>
        /// 获取图片处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiResult<List<ImagesModel>>> Handle(GetAllImgsCommand request, CancellationToken cancellationToken)
        {


             //查询图片信息
             var  imgsList= imgsRep.GetAll().Where(x=>x.ProductId==request.ProductId&&x.ImgType==request.ImgType&&!x.IsDel).ToList();


            if(imgsList==null)
            {
                return new ApiResult<List<ImagesModel>>
                {
                    code = ResultEnumCode.fails,
                    data = null,
                    message = "获取失败"

                };


            }

            return new ApiResult<List<ImagesModel>>
            {
                code = ResultEnumCode.Ok,
                data = imgsList,
                message = "获取成功"

            };
        }
    }
}
